package de.uni.muenster.idw2.location.bluetooth.service;

import android.util.Log;

public class DBToDistance {
	static Boolean fullLogModus = false;	//for internal use -> more log-messages if true

	public static double DBToDistanceFromTable(double db){
		double meter=0;
		//the factor between distance (m) and # of pixel
		//e.g. 1m = 100px  -> factor = 100
		double factor = 100;
		double absDB = Math.abs(db);
		//values based on a first (and maybe unprecise) test measurement
		if(absDB > 100)meter 		= factor * 10.00;
		else if(absDB > 90 )meter 	= factor * 7.5;
		else if(absDB > 80 )meter 	= factor * 5.0;
		else if(absDB > 70 )meter 	= factor * 3.0;
		else if(absDB > 60 )meter 	= factor * 1.0;
		else if(absDB > 50 )meter 	= factor * 0.2;
		else if(absDB > 40 )meter 	= factor * 0.1;
		else if(absDB > 30 )meter 	= factor * 0.05;
		
		if(fullLogModus)Log.i("AMK", "DBToDistance: got db:"+db+" absDB:"+absDB+" -> meter:"+meter);
		return meter;
	}
	
	public static double computeDBToDistance(double db){
		double meter = 0;
		double expon = (db + 41.832)/6.142*(-1);
		//if(fullLogModus)Log.i("AMK",Math.E+" "+expon);
		meter = Math.pow(Math.E, expon);
		//if(fullLogModus)Log.i("AMK", "DBToDistance: got db:"+db+" -> meter:"+meter);
		if (meter < 0) meter=1.;
		meter = Math.round(meter*100.)/100.;
		if(fullLogModus)Log.i("AMK", "DBToDistance: got db:"+db+" -> meter:"+meter);
		if(meter>50)meter=50;
		return meter;
	}
}
